<?php
class PaperController extends Zend_Controller_Action
{
	// 初始化
	function init()
	{
		// 取得数据库指针
		$this->view->action=$this->_getParam('action');
		$this->view->controller=$this->_getParam('controller');
		$this->view->title = '软件教研室';
		$this->view->javascript=array('jquery.inline.js', 'paper.js');
	}

	// 处理no action异常
	function __call($method, $args)
	{
		if ('Action'==substr($method, -6)) {
			$this->view->action=substr($method, 0, -6);
			$this->view->param=$this->_getAllParams();
			$this->renderScript('error/no-action.php');
		}
		else // 其他异常
			$this->_redirect('/');
	}

	/* 下面是action处理函数: ***************************/

	// 首页
	function indexAction()
	{
		$paper=new paper(); // paper module db
		$this->view->paper=$paper->find(0, '`Date` desc', 999);
	}

	// 增加对话框
	function addAction()
	{
		// 权限判断
		if (!auth::aid()) {
			$this->view->msg='你还没有登录。';
			$this->renderScript('error/small-msg.php');
			return;
		}

		$db = new AccountDb();
		$this->view->teacher = $db->Teacher(); 
		$this->view->phd = $db->Phd();
		$this->view->master = $db->Master();
		$this->view->graduate = $db->Graduate();

		$this->view->javascript=array('jquery.inline.js', 'paper.js');
	}

	// 插入一篇论文
	function insertAction()
	{
		// 权限判断
		if (!auth::aid()) {
			$this->view->msg='你还没有登录。';
			$this->renderScript('error/small-msg.php');
			return;
		}
		
		// 保存论文
		$paper=new paper();
		$param=$this->_getAllParams();
		$result=$paper->check($param);
		if ($result==-1) {
			$this->view->msg='缺少参数 "'.$paper->err_field().'" 。';
			$this->renderScript('error/small-msg.php');
			return;
		}
		else if ($result==-2) {
			$this->view->msg='参数 "'.$paper->err_field().'" 不正确。';
			$this->renderScript('error/small-msg.php');
			return;
		}
		else { // 参数验证成功，保存入数据库
			$pid=$paper->add($param);
			if ($pid>0) {
				$ret = upload::paper('file', $pid,
								Zend_Registry::get('database'));
				$this->view->msg = "论文增加成功($pid-$ret)。";
			}
			else 
				$this->view->msg='增加论文失败，请稍后再试。';

			$this->renderScript('error/small-msg.php');
		} // end of if

	}

	// 编辑论文对页面
	function editAction()
	{
		// 权限判断
		if (!auth::aid()) {
			$this->view->msg='你还没有登录。';
			$this->renderScript('error/small-msg.php');
			return;
		}

		$db = new AccountDb();
		$this->view->teacher = $db->Teacher(); 
		$this->view->phd = $db->Phd();
		$this->view->master = $db->Master();
		$this->view->graduate = $db->Graduate();

		$pid = intval($this->_getParam('PId'));
		$paper = new paper();
		$this->view->data = $paper->detail($pid);

		$this->view->javascript=array('jquery.inline.js', 'paper.js');
	}

	// 更新论文信息
	function updateAction()
	{
		$paper = new paper();
		$aid = auth::aid();
		$pid = $this->_getParam('PId');

		// 判断权限
		if (!$paper->is_author($aid, $pid) && 
				!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg='你的权限不够。';
			$this->renderScript('error/msg.php');
			return;
		}

		// 更新论文内容
		$result = $paper->update($this->_getAllParams(), $pid);
		$ret = upload::paper('file', $pid,
								Zend_Registry::get('database'));
		$this->view->msg = "论文已更新($pid-$ret)。";
		$this->renderScript('error/msg.php');
	}

};
?>
